<?php

    //验证session
    session_start();
    if(!isset($_SESSION['admin'])){
        echo '<script>alert("请先登录");location.href="index.php";</script>';
        exit;
    }

    //连接数据库
    include("../config/config.php");
    //获取用户数据
    $user=$dbh->query("SELECT * FROM user")->fetchAll();
    //获取小说数据
    $novel=$dbh->query("SELECT * FROM novel")->fetchAll();
    //获取作者
    $author=$dbh->query("SELECT * FROM author")->fetchAll();
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="/statics/js/jquery.min.js"></script>
    <title>用户编辑</title>
</head>
<style>
    table{
        width: 100%;
        margin:0 auto;
        border-collapse: collapse;
    }
    table th,table td{
        border:solid 1px black;
        text-align: center;
    }
    td input{
        width:98%;
        height: 100%;
        border: none;
        text-align: center;
    }
</style>
<body>
    <div>
        <div style="    height: 30px;">
            <div style="float: right;"><button onclick="admin_logout()">退出登录</button></div>
        </div>

        <!--  -->
        <div style="text-align: center;">
            会员管理
        </div>
        <table>
            <tr>
                <th>用户名</th>
                <th>名称</th>
                <th>积分</th>
                <th>邮箱</th>
                <th>修改</th>
                <th>删除</th>
            </tr>
            <?php

                foreach ($user as  $value) {
                    echo '
                <tr>
                    <td><input type="text" id="username'.$value['id'].'" value="'.$value['username'].'"></td>
                    <td><input type="text" id="name'.$value['id'].'" value="'.$value['name'].'"></td>
                    <td><input type="text" id="score'.$value['id'].'" value="'.$value['score'].'"></td>
                    <td><input type="text" id="email'.$value['id'].'" value="'.$value['email'].'"></td>
                    <td><button onclick="modify('."'".$value['id']."'".')">修改</button></td>
                    <td><button onclick="dele('."'".$value['id']."'".')">删除</button></td>
                </tr>
                    ';
                }
            ?>
            <tr>
                <td><input type="text" id="username"></td>
                <td><input type="text" id="name"></td>
                <td><input type="text" id="score"></td>
                <td><input type="text" id="email"></td>
                <td></td>
                <td><button onclick="add()">新增</button></td>
            </tr>
        </table>
        <div style="    height: 30px;">
        </div>
        <!--  -->

        <div style="text-align: center;">
            小说管理
        </div>
        <table>
            <tr>
                <th>小说名</th>
                <th>文件名</th>
                <th>封面图片</th>
                <th>作者</th>
                <th>类型</th>
                <th>状态</th>
                <th>热度</th>
                <th>发布时间</th>
                <th>简介</th>
                <th>修改</th>
                <th>删除</th>
            </tr>
            <?php

                foreach ($novel as  $value) {
                    echo '
                <tr>
                    <td><input type="text" id="name'.$value['id'].'" value="'.$value['name'].'"></td>
                    <td><input type="text" id="novel'.$value['id'].'" value="'.$value['novel'].'"></td>
                    <td><input type="text" id="img'.$value['id'].'" value="'.$value['img'].'"></td>
                    <td><input type="text" id="author'.$value['id'].'" value="'.$value['author'].'"></td>
                    <td><input type="text" id="type'.$value['id'].'" value="'.$value['type'].'"></td>
                    <td><input type="text" id="status'.$value['id'].'" value="'.$value['status'].'"></td>
                    <td><input type="text" id="hot'.$value['id'].'" value="'.$value['hot'].'"></td>
                    <td><input type="text" id="publish'.$value['id'].'" value="'.$value['publish'].'"></td>
                    <td><input type="text" id="content'.$value['id'].'" value="'.$value['content'].'"></td>
                    <td><button onclick="modify_novel('."'".$value['id']."'".')">修改</button></td>
                    <td><button onclick="dele_novel('."'".$value['id']."'".')">删除</button></td>
                </tr>
                    ';
                }
            ?>
            <tr>
                <td><input type="text" id="name"></td>
                <td><input type="text" id="novel"></td>
                <td><input type="text" id="img"></td>
                <td><input type="text" id="author"></td>
                <td><input type="text" id="type"></td>
                <td><input type="text" id="status"></td>
                <td><input type="text" id="hot"></td>
                <td><input type="text" id="publish"></td>
                <td><input type="text" id="content"></td>
                <td></td>
                <td><button onclick="add_novel()">新增</button></td>
            </tr>
        </table>
        <div style="color: red;">
            注：小说txt文件必须放在“/novel/source”路径下，否则程序读取不到该文件。
        </div>
        <div style="    height: 30px;">
        </div>
        <!--  -->
        <div style="text-align: center;">
            作者管理
        </div>
        <table>
            <tr>
                <th>姓名</th>
                <th>图片</th>
                <th>等级</th>
                <th>简介</th>
                <th>小说数目</th>
                <th>小说字数</th>
                <th>写作天数</th>
                <th>修改</th>
                <th>删除</th>
            </tr>
            <?php

                foreach ($author as  $value) {
                    echo '
                <tr>
                    <td><input type="text" id="name'.$value['id'].'" value="'.$value['name'].'"></td>
                    <td><input type="text" id="img'.$value['id'].'" value="'.$value['img'].'"></td>
                    <td><input type="text" id="level'.$value['id'].'" value="'.$value['level'].'"></td>
                    <td><input type="text" id="content'.$value['id'].'" value="'.$value['content'].'"></td>
                    <td><input type="text" id="novels'.$value['id'].'" value="'.$value['novels'].'"></td>
                    <td><input type="text" id="words'.$value['id'].'" value="'.$value['words'].'"></td>
                    <td><input type="text" id="workdays'.$value['id'].'" value="'.$value['workdays'].'"></td>
                    <td><button onclick="modify_author('."'".$value['id']."'".')">修改</button></td>
                    <td><button onclick="dele_author('."'".$value['id']."'".')">删除</button></td>
                </tr>
                    ';
                }
            ?>
            <tr>
                <td><input type="text" id="name"></td>
                <td><input type="text" id="img"></td>
                <td><input type="text" id="level"></td>
                <td><input type="text" id="content"></td>
                <td><input type="text" id="novels"></td>
                <td><input type="text" id="words"></td>
                <td><input type="text" id="workdays"></td>
                <td></td>
                <td><button onclick="add_author()">新增</button></td>
            </tr>
        </table>
        <!--  -->
    </div>
</body>
<script>
    function modify(id){

        if($("#username"+id).val()=="" || $("#name"+id).val()=="" || $("#score"+id).val()=="" || $("#email"+id).val()==""){
            alert("不能留空格");
            return false;
        }
        if(!window.confirm("确定修改数据？")){
            return false;
        }
        $.post(
            "../controler.php",
            {
                handle:"modify",
                id:id,
                username:$("#username"+id).val(),
                name:$("#name"+id).val(),
                score:$("#score"+id).val(),
                email:$("#email"+id).val(),
            },
            function(msg){
                if(msg=="done"){
                    location.reload();
                }
                else{
                    alert("系统出错");
                }
            }
        )
    }
    function add(){
        if($("#username").val()=="" || $("#name").val()=="" || $("#score").val()=="" || $("#email").val()=="" ){
            alert("不能留空格");
            return false;
        }
        if(!window.confirm("确定增加数据？")){
            return false;
        }
        $.post(
            "../controler.php",
            {
                handle:"add",
                username:$("#username").val(),
                name:$("#name").val(),
                score:$("#score").val(),
                email:$("#email").val(),
            },
            function(msg){
                if(msg=="done"){
                    alert("新增成功，默认密码：123")
                    location.reload();
                }
                else{
                    alert("系统出错");
                }
            }
        )
    }
    function dele(id){
        if(!window.confirm("确定删除数据？")){
            return false;
        }
        $.post(
            "../controler.php",
            {
                handle:"dele",
                id:id,
            },
            function(msg){
                if(msg=="done"){
                    location.reload();
                }
                else{
                    alert("系统出错");
                }
            }
        )
    }
    function admin_logout(){
        $.post(
            "../controler.php",
            {
                handle:"admin_logout",
            },
            function(msg){
                if(msg=="done"){
                    location.href="index.php";
                }
                else{
                    alert("系统出错");
                }
            }
        )
    }

    function modify_novel(id){
        if($("#name"+id).val()=="" || $("#novel"+id).val()=="" ){
            alert("不能留空格");
            return false;
        }
        if(!window.confirm("确定修改数据？")){
            return false;
        }
        $.post(
            "../controler.php",
            {
                handle:"modify_novel",
                id:id,
                name:$("#name"+id).val(),
                novel:$("#novel"+id).val(),
                img:$("#img"+id).val(),
                author:$("#author"+id).val(),
                type:$("#type"+id).val(),
                status:$("#status"+id).val(),
                hot:$("#hot"+id).val(),
                publish:$("#publish"+id).val(),
                content:$("#content"+id).val(),
            },
            function(msg){
                if(msg=="done"){
                    location.reload();
                }
                else{
                    alert("系统出错");
                }
            }
        )
    }
    function add_novel(){
        if($("#name").val()=="" || $("#novel").val()=="" ){
            alert("不能留空格");
            return false;
        }
        if(!window.confirm("确定增加数据？")){
            return false;
        }
        $.post(
            "../controler.php",
            {
                handle:"add_novel",
                name:$("#name").val(),
                novel:$("#novel").val(),
                img:$("#img").val(),
                author:$("#author").val(),
                type:$("#type").val(),
                status:$("#status").val(),
                hot:$("#hot").val(),
                publish:$("#publish").val(),
                content:$("#content").val(),
            },
            function(msg){
                if(msg=="done"){
                    alert("新增成功，请将小说文件放到相应文件夹下")
                    location.reload();
                }
                else{
                    alert("系统出错");
                }
            }
        )
    }

    function dele_novel(id){
        if(!window.confirm("确定删除数据？")){
            return false;
        }
        $.post(
            "../controler.php",
            {
                handle:"dele_novel",
                id:id,
            },
            function(msg){
                if(msg=="done"){
                    location.reload();
                }
                else{
                    alert("系统出错");
                }
            }
        )
    }

</script>
</html>